layui.use(['layer','form','laydate','element','upload'], function(){ 
  var  layer = layui.layer; 
   var form = layui.form;
    var element = layui.element;

 var laydate = layui.laydate;
 
 


var upload = layui.upload	  

 upload.render({
    elem: '.file'
    ,url: '../upload/file.asp'
	,accept: 'file'
	
    ,before: function(obj){
	layer.load(); 
    }
    ,done: function(res){
			 console.log("123") 
		
		  layer.closeAll('loading');
      //如果上传失败
      if(res.code > 0){
        return layer.msg(res.msg);
      }
      //上传成功
	    if(res.code == 0){
		
	var item=this.item
		console.log(item)
item.parent(".layui-upload").find(".file-div").html('<div class="upload-file">'+res.data["src"]+"<input type='hidden' name='fujian' value="+res.data["src"]+"  /><div class='file-close' onclick='fjclose(this)'>×</div></div>");

	
        return layer.msg(res.msg);
		
      }
    }
    ,error: function(){
		  layer.closeAll('loading');
      //演示失败状态，并实现重传
     return layer.msg('上传失败');
    
    }
  });	

 
 
 
upload.render({
    elem: '.upload'
    ,url: '../upload/index.asp'
	,accept:'images'
	,acceptMime:'image/*'
	// ,multiple: true
     ,auto: false
    , bindAction: "#btnHide"
	 ,before: function(obj){
	layer.load(); 
    }
      , choose: function(obj) {
       var files = obj.pushFile();
                        var index, file, indexArr = [];
                        for(index in files) {
                            indexArr.push(index);
                        };
                        var iaLen = indexArr.length;
                        file = files[indexArr[iaLen - 1]];
                        for(var i = 0; i < iaLen - 1; i++) {
                            delete files[indexArr[i]];
                        }
                        try {
                            if(file.size > 500 * 1024) {
                                delete files[index];
                                photoCompress(file, {
                                    quality: 0.3,
                                }, function(base64Codes) {
                                    var bl = convertBase64UrlToBlob(base64Codes);
                                    obj.resetFile(index, bl, file.name);
                                    $("#btnHide").trigger("click");
                                });
                            } else {
                                $("#btnHide").trigger("click");
                            }
                        } catch(e) {
                            $("#btnHide").trigger("click");
                        }
                    }
	
	
    ,done: function(res){
		  layer.closeAll('loading');
      //如果上传失败
      if(res.code > 0){
        return layer.msg(res.msg);
      }
      //上传成功
	    if(res.code == 0){
		
	var item=this.item
		console.log(item)
		item.parent(".layui-upload").find(".upload-div").show(500)
		item.parent(".layui-upload").find(".upload-div").append("<div class='upload-img'><img src=../"+res.data["src"]+" width=70 height=70><input type='hidden' name='pic' value="+res.data["src"]+"  /><div class='upload-close' onclick='picclose(this)'>×</div></div>");

		
	
        return layer.msg(res.msg);
		
      }
    }
    ,error: function(){
		  layer.closeAll('loading');
      //演示失败状态，并实现重传
     return layer.msg('上传失败');
    
    }
  });
  

 
 
 
 
 
form.verify({
    
content: [/^[\w\W]{2,500}$/, '评论为2到255个字符']
     ,username: [/^[\u4E00-\u9FA5\uf900-\ufa2d\w\.]{1,12}$/, '姓名为1到12个字符']
  });
  
form.on('submit(mes)', function (data) {
var rowNum=$('.content').val().split(/\r?\n|\r/).length
	
if(rowNum>20){
			layer.msg('不要超过20行')
}else
{message()}

     
}) 

form.on('submit(reg)', function () {
reg()
}) 
form.on('submit(login)', function () {
login_user()
})  
   
//$("#c_div").click(function (e) {
//    if (!$(e.target).closest(".c_form,upload-div").length) {
//        $("#c_div").css("display","none");
//    }
//});

$(".no_btn").click(function (e) {
  $("#c_div").hide();
});








});

function en(content) {
  result = '';
  for (var i=0; i<content.length; i++)
  result+='&#' + content.charCodeAt(i) + ';';
  return result;
}

function cn(content) {
  var code = content.match(/&#(\d+);/g);
  result= '';
  for (var i=0; i<code.length; i++)
  result += String.fromCharCode(code[i].replace(/[&#;]/g, ''));
 return result;
}


function tc(my_tit,my_url){
var index = layer.open({
title: my_tit,
type: 2,
content: my_url,
 fixed: true
 
 
});
layer.full(index);

}

function fjclose(obj){

 var my_id=$(obj).prev("input[name=fujian]").val()


$.ajax({
			type : "POST",
			cache : true,
			dataType : "json",
			url : "../inc/ajax.asp?act=dell_file",
			data:{"id":my_id},
			error:function(){alert("fail");},
            success:function(data){}
	
	});
	$(obj).parents(".upload-file").remove(); 	 
	 
	 }	

function picclose(obj){
var my_id=$(obj).prev("input[name=pic]").val()

$.ajax({
			type : "POST",
			cache : true,
			dataType : "json",
			url : "../inc/ajax.asp?act=dell_file",
			data:{"id":my_id},
			error:function(){alert("fail");},
            success:function(data){}
	
	});
	$(obj).parents(".upload-img").remove(); 
} 

	 
	 
function picupload(){
	
	console.log("11")
layui.use('upload', function(){
var upload = layui.upload	  

 upload.render({
    elem: '.file'
    ,url: '../upload/file.asp'
	,accept: 'file'
	
    ,before: function(obj){
	layer.load(); 
    }
    ,done: function(res){
			 console.log("123") 
		
		  layer.closeAll('loading');
      //如果上传失败
      if(res.code > 0){
        return layer.msg(res.msg);
      }
      //上传成功
	    if(res.code == 0){
		
	var item=this.item
		console.log(item)
item.parent(".layui-upload").find(".file-div").html('<div class="upload-file">'+res.data["src"]+"<input type='hidden' name='fujian' value="+res.data["src"]+"  /><div class='file-close' onclick='fjclose(this)'>×</div></div>");

	
        return layer.msg(res.msg);
		
      }
    }
    ,error: function(){
		  layer.closeAll('loading');
      //演示失败状态，并实现重传
     return layer.msg('上传失败');
    
    }
  });	

 
 
 
upload.render({
    elem: '.upload'
    ,url: '../upload/index.asp'
	,accept:'images'
	,acceptMime:'image/*'
	// ,multiple: true
     ,auto: false
    , bindAction: "#btnHide"
	 ,before: function(obj){
	layer.load(); 
    }
      , choose: function(obj) {
       var files = obj.pushFile();
                        var index, file, indexArr = [];
                        for(index in files) {
                            indexArr.push(index);
                        };
                        var iaLen = indexArr.length;
                        file = files[indexArr[iaLen - 1]];
                        for(var i = 0; i < iaLen - 1; i++) {
                            delete files[indexArr[i]];
                        }
                        try {
                            if(file.size > 500 * 1024) {
                                delete files[index];
                                photoCompress(file, {
                                    quality: 0.3,
                                }, function(base64Codes) {
                                    var bl = convertBase64UrlToBlob(base64Codes);
                                    obj.resetFile(index, bl, file.name);
                                    $("#btnHide").trigger("click");
                                });
                            } else {
                                $("#btnHide").trigger("click");
                            }
                        } catch(e) {
                            $("#btnHide").trigger("click");
                        }
                    }
	
	
    ,done: function(res){
		  layer.closeAll('loading');
      //如果上传失败
      if(res.code > 0){
        return layer.msg(res.msg);
      }
      //上传成功
	    if(res.code == 0){
		
	var item=this.item
		console.log(item)
		item.parent(".layui-upload").find(".upload-div").show(500)
		item.parent(".layui-upload").find(".upload-div").append("<div class='upload-img'><img src=../"+res.data["src"]+" width=70 height=70><input type='hidden' name='pic' value="+res.data["src"]+"  /><div class='upload-close' onclick='picclose(this)'>×</div></div>");

		
	
        return layer.msg(res.msg);
		
      }
    }
    ,error: function(){
		  layer.closeAll('loading');
      //演示失败状态，并实现重传
     return layer.msg('上传失败');
    
    }
  });
  
	 });
  }
  
  
function mestc(my_tit,upid){
$("input[name=upid]").val(upid);
$("textarea[name=content]").attr("placeholder",my_tit);   
$("#c_div").show(200);
if(upid>0){
	$(".layui-upload").hide();
	$(".f_type").hide();  
	}	
	else
	{$(".layui-upload").show();
	$(".f_type").show(); 
	 }
	  

checknum()
$("textarea").focus()

}


function mesto(sid,upid,str){
$("input[name=class]").val(2);
$("input[name=upid]").val(upid);
$("input[name=name]").val(sid); 
$("textarea[name=content]").attr("placeholder",str);   
$("#c_div").show(200);


$(".f_type").hide();  

checknum()
 $("textarea").focus()
 
}




  
 function checknum()
 {
	 
var lenInput = $('.content').val().length;
	$('.textareaInput').html(lenInput);
	 
 $(".content").on("keyup change", function(){
			lenInput = $(this).val().length;
			if(lenInput>5 && lenInput<=500){
				$('.textareaInput').html(lenInput);
				
			}else{
			
			}
		});
		
		} 
  
  
 




function down_file(id){

$.ajax({
			type : "POST",
			cache : true,
			dataType : "json",
			url : "../inc/ajax.asp?act=down_file",
			data:{"id":id},
			error:function(){alert("fail");},
            success:function(data){
			switch(data.status)
		{
			case "n":
			layer.msg(data.info)
			break;
			case "y":
	   location.href='../'+data.file;  
			break;
			
				
		}
				
				}
	
	});
		 
	 
	 }	



function set(zd,zt,my_id,info) {
layer.confirm(info, function(index){
      
		$.ajax({
			type : "POST",
			cache : true,
			dataType : "json",
			url : "../inc/ajax.asp?act=set",
			data:{"id":my_id,"zd":zd,"zt":zt},
			error:function(){alert("fail");},
            success:function(data)
	{
		
		switch(data.status)
		{
			case "n":
			layer.msg(data.info)
			break;
			case "y":
	      location.reload();
			break;
			
				
		}
		
	}
	
	});
	
		
        layer.close(index);
      });

}	


function message() {
	
	
      $.ajax({
			type : "POST",
			dataType : "json",
			cache : true,
			url : "../inc/ajax.asp?act=comment",
		     data: $("#cform").serialize(),
			error:function(){alert("fail");},
            success:function(data)
	           {
		   if(data.status=='y')
				{
            location.href=data.url;
				}
				else{
				layer.msg(data.info)}
		
	}
	
	});
	
		

}

	
function del(my_table,my_id) {
layer.confirm('真的删除行么', function(index){
      
$.ajax({
			type : "POST",
			cache : true,
			dataType : "json",
			url : "../inc/ajax.asp?act=del",
			data:{"table":my_table,"id":my_id},
			error:function(){alert("fail");},
            success:function(data)
	{
		switch(data.status)
		{
			case "n":
			layer.msg(data.info)
			break;
			case "y":
		
	      location.reload();
			break;
			
				
		}
		
	}
	
	});
	
		
        layer.close(index);
      });

}	


function dell(obj,my_id) {
layer.confirm('真的删除行么', function(index){
      
$.ajax({
			type : "POST",
			cache : true,
			dataType : "json",
			url : "../inc/ajax.asp?act=dell",
			data:{"id":my_id},
			error:function(){alert("fail");},
            success:function(data)
	{
		switch(data.status)
		{
			case "n":
			layer.msg(data.info)
			break;
			case "y":
			$(obj).parents(".c_show").remove()
	   
			break;
			
				
		}
		
	}
	
	});
	
		
        layer.close(index);
      });

}	


function del_reply(obj,my_id,upid) {
layer.confirm('真的删除行么', function(index){
      
$.ajax({
			type : "POST",
			cache : true,
			dataType : "json",
			url : "../inc/ajax.asp?act=del_reply",
			data:{"id":my_id,"upid":upid},
			error:function(){alert("fail");},
            success:function(data)
	{
		switch(data.status)
		{
			case "n":
			layer.msg(data.info)
			break;
			case "y":
			$(obj).parents(".reply_list").remove()
	   
			break;
			
				
		}
		
	}
	
	});
	
		
        layer.close(index);
      });

}	

	
function zan(obj,the)
{
	
	 $.ajax({
			type : "POST",
			dataType : "json",
			cache : true,
			url : "../inc/ajax.asp?act=zan",
		   data:{"id":the},
			error:function(){alert("fail");},
            success:function(data)
	           {
				
		   if(data.status=='y')
				{
			 $(obj).addClass("active");
            $(obj).children("b").html(data.info)
				}
				else{
				layer.msg(data.info)}
		
	}
	
	});
	
	
}

function login()
{
	
	 $.ajax({
			type : "POST",
			dataType : "json",
			cache : true,
			url : "../inc/ajax.asp?act=login",
		  data: $("#aform").serialize(),
			error:function(){alert("fail");},
            success:function(data)
	           {
				
		   if(data.status=='y')
				{
			   location.href='index.asp';
				}
				else{
				layer.msg(data.info)}
		
	}
	
	});
	
	
}

	

function qd(){

$.ajax({
			type : "POST",
			cache : true,
			dataType : "json",
			url : "../inc/ajax.asp?act=qd",
			data:{},
			error:function(){alert("fail");},
            success:function(data){
			switch(data.status)
		{
			case "n":
			
			break;
			case "y":
	   location.reload();
			break;
			
				
		}
				
				}
	
	});
		 
	 
	 }	


function pic()
{
	 
		 $(".galleryOverlay").remove()
 $(".c_pic").each(function(){
        $(this).find("a").touchTouch();
    });

  
	 $('.readmore').readmore({maxHeight: 75});
	
}



				
$(function(){
	
	var startHref;	
	

function item_masonry()
	{
		$('.item_list img').load(function(){$('.list-loop').masonry({itemSelector:'.loop'});});
		$('.list-loop').masonry({itemSelector:'.loop'});
	}
	
	var p=false;
	if($(".item_list").length>0){p=true;item_masonry();}
	$(".pagemore a").click(function(){
		
		var href=$(this).attr("href");
		startHref=href;
		if(href!=undefined){
			$.ajax({
				type:"get",
				cache:false,
				url:startHref,
				success:function(data){
					var $result=$(data).find(".loop");
					if(p){$(".list-loop").append($result).masonry('appended',$result);item_masonry();}else{$(".list-loop").append($result);}
					var newHref=$(data).find(".pagemore a").attr("href");
					if(newHref!=""){$(".pagemore a").attr("href",newHref);}else{$(".pagemore").html("已显示全部内容");}
			 emo()
			pic()
				}
			})
			
		}
		return false;
		
	})
	
$(window).bind("scroll",function(){
		if($(document).scrollTop()+$(window).height()>$(document).height()-70){
		if($(".pagemore a").attr('href')!=startHref){$(".pagemore a").trigger("click");}}
	})
	


		

	
});

var InterValObj; //timer变量，控制时间
var count = 60; //间隔函数，1秒执行
var curCount;//当前剩余秒数

var codeLength = 6;//验证码长度
function sendMessage() {
curCount = count;
var dealType; //验证方式
var uid=$("#email").val();//用户uid


//产生验证码
if (uid == "") { 
layer.msg("请输入邮箱！"); 

return false; 
} 

if (!uid.match( /^([\w\_\-]+)@([\w\-]+[\.]?)*[\w]+\.[a-zA-Z]{2,10}$/ )) { 
alert("邮箱格式不正确！"); 

return false; 
} 

 
//设置button效果，开始计时
	$("#btnSendCode").attr("disabled", "true");
	$("#btnSendCode").val( + curCount + "秒再获取");
	InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
//向后台发送处理数据
	$.ajax({
		type: "post", //用POST方式传输
		dataType: "json", //数据格式:JSON
		url : "../inc/ajax.asp?act=yzm",
		data:{"uid":uid},
		error: function () { },
		success: function (data){
		if(data.status=='y')
{
	layer.msg('验证码已发送')
}else
{
	layer.msg(data.info)
clearInterval(InterValObj)
$("#btnSendCode").removeAttr("disabled");
$("#btnSendCode").val("获取验证码");
	}
		
	
	
			
			}
	});
}
//timer处理函数
function SetRemainTime() {


	if (curCount == 0) {                
		window.clearInterval(InterValObj);//停止计时器
		$("#btnSendCode").removeAttr("disabled");//启用按钮
		$("#btnSendCode").val("重新发送验");
		    
	}
	else {
		curCount--;
		$("#btnSendCode").val( + curCount + "秒再获取");
	}
}

function user_login(){

	$("#user").show(500);
	
	 }	
	 
function login_div(){
$("#user .atit").html('会员登陆');
	$("#reg").hide();
	$("#login").show();
	
	 }	 
	  
function reg_div(){
$("#user .atit").html('会员注册');
	$("#login").hide();
	$("#reg").show();
	
	 }

	  
function reg()
{
	
	 $.ajax({
			type : "POST",
			dataType : "json",
			cache : true,
			url : "../inc/ajax.asp?act=reg",
		  data: $("#rform").serialize(),
			error:function(){alert("fail");},
            success:function(data)
	           {
				
		   if(data.status=='y')
				{
			   layer.msg(data.info)
			    login_div()
			   $("input[name=email]").val("")
			     $("input[name=code]").val("")
				  $("input[name=code1]").val("")
				$("input[name=username]").val("")
				}
				else{
				layer.msg(data.info)}
		
	}
	
	});
	
	
}

function login_user()
{
	
 $.ajax({
			type : "POST",
			dataType : "json",
			cache : true,
			url : "../inc/ajax.asp?act=login_user",
		  data: $("#lform").serialize(),
			error:function(){alert("fail");},
            success:function(data)
	           {
				
		   if(data.status=='y')
				{
			 location.reload();
				}
				else{
				layer.msg(data.info)}
		
	}
	
	});
	
	
}
function login_close(){

	$(".abg").hide(300);
	
	 } 